/**
 * Copyright (C) 2008 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.mail.sender.gmail;

import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;

import cc.aileron.mail.sender.MailFrom;
import cc.aileron.mail.sender.MailSenderModule;

import com.google.inject.Provider;

/**
 * GMail送信モジュール
 * 
 * @author Aileron
 * 
 */
public class GMailSenderModule extends MailSenderModule
{
    /**
     * static
     */
    static
    {
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    }

    /**
     * MailFromの生成
     * 
     * @return 生成されたMailFrom
     * @throws UnsupportedEncodingException
     */
    private MailFrom newFrom() throws UnsupportedEncodingException
    {
        final InternetAddress fromAddress;
        final Session session;

        final Properties p = new Properties();
        p.setProperty("mail.smtp.host", "smtp.gmail.com");
        p.setProperty("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        p.setProperty("mail.smtp.socketFactory.fallback", "false");
        p.setProperty("mail.smtp.port", "465");
        p.setProperty("mail.smtp.socketFactory.port", "465");
        p.setProperty("mail.smtp.auth", "true");

        final PasswordAuthentication authentication = new PasswordAuthentication(
                account.user, account.pass);
        session = Session.getInstance(p, new Authenticator()
        {
            @Override
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return authentication;
            }
        });

        fromAddress = new InternetAddress(account.user, account.user,
                setting.encoding);
        return new MailFrom(session, fromAddress);
    }

    /*
     * (非 Javadoc)
     * 
     * @see cc.aileron.mail.sender.MailSenderModule#getMailFromProvider()
     */
    @Override
    protected Provider<MailFrom> getMailFromProvider()
    {
        return provider;
    }

    /**
     * constractor
     * 
     * @param account
     * @throws Exception
     */
    public GMailSenderModule(final GMailAccount account) throws Exception
    {
        this.account = account;
        final MailFrom from = newFrom();
        this.provider = new Provider<MailFrom>()
        {
            public MailFrom get()
            {
                return from;
            }
        };
    }

    /**
     * account
     */
    private final GMailAccount account;

    /**
     * from
     */
    private final Provider<MailFrom> provider;
}